package com.nurese.controller;

import com.nurese.entity.Careworker;
import com.nurese.entity.Order;
import com.nurese.entity.Order;
import com.nurese.entity.User;
import com.nurese.service.CareworkerService;
import com.nurese.service.OrderService;
import com.nurese.service.OrderService;
import com.nurese.service.UserService;
import com.nurese.utils.BeanMapUtils;
import com.nurese.utils.MapControll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    private final String LIST="Order/list";
    private final String ADD="Order/add";
    private final String UPDATE="Order/update";



    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private CareworkerService careworkerService;

    @GetMapping("/add")
    public String create(ModelMap modelMap){
        List<User> users=userService.query(null);
        modelMap.addAttribute("users",users);
        List<Careworker> careworkers=careworkerService.query(null);
        modelMap.addAttribute("careworkers",careworkers);
        return ADD;
    }

    @PostMapping("/create")
    @ResponseBody
//    public void  create(Order order){
//        orderService.create(order);
//    }
    public Map<String,Object> create(Order order){
        System.out.println("++++4:"+ BeanMapUtils.beanToMap(order));
//        orderService.create(order);
        int result= orderService.create(order);
        if (result<=0){
//            新增失败
            return MapControll.getInstance().error().getMap();

        }
        return MapControll.getInstance().success().getMap();
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public Map<String,Object> delete(@PathVariable("id") Integer id){
        int result = orderService.delete(id);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }
    @PostMapping("/delete")
    @ResponseBody
    public Map<String,Object> delete(String ids){
        int result = orderService.delete(ids);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @PostMapping("/update")
    @ResponseBody
//    @RequestBody
    public Map<String,Object> update(Order order){
        System.out.println("======22"+order);
        int result = orderService.update(order);
        if(result<=0){
            return MapControll.getInstance().error().getMap();
        }
        return MapControll.getInstance().success().getMap();
    }


    @GetMapping ("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap){

        Order order=orderService.detail(id);
        modelMap.addAttribute("order",order);
        List<User> users=userService.query(null);
        modelMap.addAttribute("users",users);
        List<Careworker> careworkers=careworkerService.query(null);
        modelMap.addAttribute("careworkers",careworkers);
        return UPDATE;
    }


    @PostMapping("/query")
    @ResponseBody
    public Map<String,Object> query(Order order){
        System.out.println("++++order:"+BeanMapUtils.beanToMap(order));
//        List<Order> list =orderService.query(order);
        List<Order> orders =orderService.query(order);
        List<User> users=userService.query(null);
        List<Careworker> careworkers=careworkerService.query(null);
        orders.forEach(entity->{
            users.forEach(user -> {
                if(user.getId() == entity.getUserid().intValue()){
                    entity.setUser(user);
                }
            });
            careworkers.forEach(careworker -> {
                if(careworker.getId() == entity.getWorkerid().intValue()){
                    entity.setCareworker(careworker);
                }
            });
        });

        System.out.println("====orders:"+orders);

        Integer count=orderService.count(order);
        return MapControll.getInstance().success().put("data",orders).put("count",count).getMap();
    }

    @GetMapping("/list")
    public String list(){

        return LIST;
    }
}

